package com.lagouedu.service.impl;

import com.aliyun.oss.OSSClient;
import com.aliyun.oss.model.DownloadFileRequest;
import com.aliyun.oss.model.DownloadFileResult;
import com.aliyun.oss.model.OSSObject;
import com.lagouedu.bean.UploadResult;
import com.lagouedu.config.AliyunConfig;
import com.lagouedu.service.FileService;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.UUID;

@Service
public class FileServiceImpl implements FileService {

    @Autowired
    private AliyunConfig aliyunConfig;

    @Autowired
    private OSSClient ossClient;

    private static final String[] IMAGE_TYPE = new String[]{ ".jpg",".jpeg", ".png"};

    @Override
    public UploadResult upload(MultipartFile multipartFile) {
        // 校验图片格式
        boolean isLegal = false;
        for (String s : IMAGE_TYPE) {
            if(StringUtils.endsWithIgnoreCase(multipartFile.getOriginalFilename(),s)){
                isLegal = true;
                break;
            }
        }
        UploadResult uploadResult = new UploadResult();
        if(!isLegal){
            uploadResult.setStatus("error");
            uploadResult.setResponse("不支持的文件格式！");
            return uploadResult;
        }
        String fileName = multipartFile.getOriginalFilename();
        String filePath = getFilePath(fileName);
        try {
            ossClient.putObject(aliyunConfig.getBucketName(),filePath,new ByteArrayInputStream(multipartFile.getBytes()));
        } catch (IOException e) {
            e.printStackTrace();
            uploadResult.setStatus("error");
            return uploadResult;
        }
        uploadResult.setStatus("success");
        uploadResult.setName(this.aliyunConfig.getUrlPrefix()+ filePath);
        uploadResult.setUid(String.valueOf(System.currentTimeMillis()));
        return uploadResult;
    }

    public OSSObject downLoadFile(String key){
        OSSObject ossObject = new OSSObject();
        try {
            ossObject = ossClient.getObject(aliyunConfig.getBucketName(), key);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
        return ossObject;
    }

    public Boolean deleteFile(String key){
        try {
            ossClient.deleteObject(aliyunConfig.getBucketName(), key);
        } catch (Exception e){
            System.out.println(e);
            return false;
        }
        return true;
    }


    private String getFilePath(String sourceFileName) {
        DateTime dateTime = new DateTime();
        return "/images/" + dateTime.toString("yyyy")+
                "/" + dateTime.toString("MM") +
                "/"+ dateTime.toString("dd") +
                "/" + UUID.randomUUID().toString() +
                "." +StringUtils.substringAfterLast(sourceFileName, ".");
    }
}
